
const state = {
  count: 2
}
// mutations中的内容就是用来修改state数据的,不能有for、if和异步
const mutations = {
  ADD(state) {
    state.count = state.count + 1
  },
  SUB(state) {
    state.count = state.count - 1
  }
}
// actions函数中有可以有if,for和异步
const actions = {
  increment({ commit }) {
    commit('ADD')
  },
  decrement({ commit }) {
    commit('SUB')
  },
  isOddIncrement({ commit, state }) {
    if (state.count % 2 == 1) {
      commit('ADD')
    }
  },
  asyncIncreament({ commit }) {
    setTimeout(() => {
      commit('ADD')
    }, 1000)
  }
}

const getters = {
  tenfoldCount(state) {
    return 10 * state.count
  }
}

export default {
  state,
  mutations,
  actions,
  getters,
}